--[[
FilePath: \Lua\View\UICoupon\UICoupon.lua
内测充值或等级达到返利
--]]

require 'View/UIBase/UIBase'

UICoupon = UIBase.New(PanelNames.UICoupon)
local _M = UICoupon

_M.hasInit = false
_M.isGet = false

function _M.Init()
    UIBase.Init(_M)
end

function _M:OnCreate()
    if not _M.hasInit then
        self:RegisterOnClick(self:GetChild("bgClose"), self.OnBack)
        self:RegisterOnClick(self:GetChild("btn"), self.OnBack)
        self:RegisterOnClick(self:GetChild("close"), self.OnBack)
        self.itemList = self:GetChild("itemList");
        self.tips = self:GetChild("Tips");
        self.text = self:GetChild("Text");
        self.text1 = self:GetChild("Text1");
        self.getReward = self:GetChild("get");
        self.typeController = self:GetController("Type");
        self.voucherReward = G.dataTable["t_voucherreward"];
        _M.hasInit = true

        self:addEvent()
    end
end

function _M:addEvent()
    self.getReward.onClick:Add(self.GetReward)
    ListenEvent(Config.EventType.Fresh_FuncMenuData, self.updateView)
end

function _M:removeEvent()
    self.getReward.onClick:Remove(self.GetReward)
    CloseEvent(Config.EventType.Fresh_FuncMenuData, self.updateView);
end

function _M.GetReward()
    if _M.isGet then
        return;
    end
    _M.isGet =  true;
    costHandler.ReqFetchVoucher();
end

function _M.updateView()
    _M:refreshView();
end

function _M:refreshView()
    self.isGet = false;
    local serverType = logicMgr.CostManager.GetServerRebateType()
    if serverType == 0 then
        self:OnBack();
    end
    local cfg;
    local uid;
    if logicMgr.LoginManager.user_id then
        uid = logicMgr.LoginManager.user_id;
        cfg = self.voucherReward[tostring(uid)];
    end
    local voucherData = logicMgr.CostManager.GetVoucherData();
    if serverType == 2 then
        if voucherData ~= nil and voucherData.rewarded == 0 then
            self.typeController.selectedIndex = 1;
        else
            self.typeController.selectedIndex = 2;
        end

        self.text.text = getLanguage("ActivityDesc1004");
        if cfg ~= nil then
            local award = decodeJsonStrToTabel(cfg.f_Rebate);
            logicMgr.CostManager.RefreshItemList(self.itemList, award);
            self.text1.text = string.format(getLanguage("ActivityDesc1005"),cfg.f_Recharge);
        else
            self:OnBack();
        end
    else
        self.typeController.selectedIndex = 0;

        local num = 0;
        if cfg ~= nil then
            num = num + tonumber(cfg.f_Recharge)
        end
        if voucherData ~= nil and voucherData.recharge ~= nil then
            num = num + tonumber(voucherData.recharge)
        end

        self.itemList:RemoveChildrenToPool()
        local item = self.itemList:AddItemFromPool()
        local ctrlItem = CtrlManager.GetCtrl(PanelNames.Item);
        local itemData = logicMgr.CostManager.getItemOrEquipDataByIdAndNum(200028, math.ceil(num * 1.5));
        if ctrlItem ~= nil then
            ctrlItem.initData(item,itemData);
        end
        item = self.itemList:AddItemFromPool()
        itemData = logicMgr.CostManager.getItemOrEquipDataByIdAndNum(200002, 20);
        if ctrlItem ~= nil then
            ctrlItem.initData(item,itemData);
        end

        self.text1.text = string.format(getLanguage("ActivityDesc1003"), num);
        self.tips.text = getLanguage("ActivityDesc1001");
        self.text.text = getLanguage("ActivityDesc1002");
    end
end

-- 显示
function _M:OnEnable()
    if self.hasInit and self.voucherReward ~= nil then
        --返利信息
        costHandler.ReqVoucherData();
        self:refreshView();
    else
        self:OnBack();
    end
end

-- 隐藏
function _M:OnDisable()

end

-- 卸载
function _M:OnDestroy()
    self:removeEvent();
    _M.hasInit = false
    _M.isGet = false
end

return _M;